<template>
  <div>
    <el-form ref="form" label-width="80px" :model="skuInfo">
      <el-form-item label="SPU名称"> {{ spu.spuName }}</el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          v-model="skuInfo.price"
          type="number"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          rows="4"
          placeholder="规格描述"
          resize="none"
          v-model="skuInfo.skuDesc"
        ></el-input>
      </el-form-item>

      <el-form-item label="平台属性">
        <el-form :inline="true" ref="inlineForm" label-width="80px">
          <el-form-item
            :label="attr.attrName"
            v-for="attr in attrInfoList"
            :key="attr.id"
          >
            <el-select placeholder="请选择" v-model="attr.attrIdAndValue">
              <el-option
                :label="attrValue.valueName"
                :value="`${attr.id}:${attrValue.id}`"
                v-for="attrValue in attr.attrValueList"
                :key="attrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="销售属性">
        <el-form :inline="true" ref="inlineForm" label-width="80px">
          <el-form-item
            :label="sale.saleAttrName"
            v-for="sale in spuSaleAttrList"
            :key="sale.id"
          >
            <el-select placeholder="请选择" v-model="sale.attrIdAndValueId">
              <el-option
                :label="saleValue.saleAttrValueName"
                :value="`${sale.id}:${saleValue.id}`"
                v-for="saleValue in sale.spuSaleAttrValueList"
                :key="saleValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="图片列表">
        <el-table
          style="width: 100%"
          border
          :data="spuImageList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="prop" type="selection" width="80">
          </el-table-column>
          <el-table-column prop="prop" label="图片" width="width">
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="图片" width="操作">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                v-if="row.isDefault == 0"
                @click="changeDefault(row)"
                >设置默认</el-button
              >
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>

      <el-button type="primary" @click="save">保存</el-button>
      <el-button style="margin-left: 20px" @click="cancel">取消</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      spuImageList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      //须要收集的数据
      skuInfo: {
        category3Id: 0,
        price: "",
        spuId: 0,
        tmId: 0,
        weight: "",
        skuName: "",
        skuDesc: "",
        //默认图片
        skuDefaultImg: "",
        skuAttrValueList: [
          {
            attrId: 0,
            valueId: 0,
          },
        ],

        skuImageList: [
          {
            imgName: "",
            imgUrl: "",
            isDefault: "",
            spuImgId: 0,
          },
        ],

        skuSaleAttrValueList: [
          {
            saleAttrId: 0,
            saleAttrValueId: 0,
          },
        ],
      },

      spu: {},

      //收集图片字段
      imgList: [],
    };
  },
  methods: {
    async getData(category1Id, category2Id, spu) {
      //收集父组件传递的数据
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.tmId = spu.tmId;
      this.skuInfo.skuId = spu.skuId;
      this.spu = spu;
      let result1 = await this.$API.sku.reqSpuImageList(spu.id);
      let item = result1.data;
      item.forEach((element) => {
        element.isDefault = 0;
      });
      this.spuImageList = item;
      let result2 = await this.$API.sku.reqSpuSaleAttrList(spu.id);
      this.spuSaleAttrList = result2.data;
      let result3 = await this.$API.sku.reqAttrInfoList(
        category1Id,
        category2Id,
        spu.category3Id
      );
      this.attrInfoList = result3.data;
    },

    //table表格选择勾选对应的事件
    handleSelectionChange(params) {
      //缺少isDefalut字段
      this.imgList = params;
      console.log(params);
    },

    //是否为默认
    changeDefault(row) {
      this.spuImageList.forEach((item) => {
        item.isDefault = 0;
      });
      row.isDefault = 1;
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },

    //取消操作
    cancel() {
      this.$emit("changeScenes", 0);
      //清除数据
      Object.assign(this._data, this.$options.data());
    },

    //点击保存
    async save() {
      const { attrInfoList, skuInfo, spuSaleAttrList, imgList } = this;
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValue) {
          const [attrId, valueId] = item.attrIdAndValue.split(":");
          prev.push({ attrId, valueId });
        }
        return prev;
      }, []);

      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] =
            item.attrIdAndValueId.split(":");
          prev.push({ saleAttrId, saleAttrValueId });
        }
        return prev;
      }, []);

      skuInfo.skuImageList = imgList.map((itme) => {
        return {
          imgName: item.imgName,
          imgUrl: itme.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });

      let result = await this.$API.sku.reqAddSku(skuInfo);
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: result.message,
        });
        this.cancel();
      }
    },
  },
};
</script>

<style>
</style>